package com.huaxin.hxcloud.fragment;

import android.app.Dialog;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.huaxin.common.BaseActivity;
import com.huaxin.common.BaseFragment;
import com.huaxin.common.Entity.BaseDTO;
import com.huaxin.common.Entity.UserDTO;
import com.huaxin.common.Global;
import com.huaxin.common.utils.DialogUtil;
import com.huaxin.common.utils.ToastUtil;
import com.huaxin.hxcloud.R;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author songxf
 * @date 2017/10/12
 * @Desc 简介Fragment
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class HxIntroFragment extends BaseFragment {
    @BindView(R.id.more)
    TextView    mMore;
    @BindView(R.id.toolbar)
    Toolbar     mToolbar;
    @BindView(R.id.et_nick_name)
    EditText    mEtNickName;
    @BindView(R.id.bt_del)
    ImageButton mBtDel;
    Unbinder unbinder;
    UserDTO  _user;

    @Override
    protected void onResumeView() {
        super.onResumeView();
        ((BaseActivity) getActivity()).setSupportActionBar(mToolbar);
        mToolbar.setTitle("简介");
        _user = Global.user;
        mEtNickName.setText(_user.getUserIntroduction());
    }


    @Override
    protected int getContentViewId() {
        return R.layout.fragment_modify_intro;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mMore.setText("完成");
        mMore.setVisibility(View.VISIBLE);
        mEtNickName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if (TextUtils.isEmpty(s)) {
                    mBtDel.setVisibility(View.GONE);
                    mMore.setText("取消");
                } else {
                    mBtDel.setVisibility(View.VISIBLE);
                    mMore.setText("确认");
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @OnClick({R.id.bt_del, R.id.more})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_del:
                mEtNickName.getText().clear();
                break;
            case R.id.more:
                if (TextUtils.isEmpty(mEtNickName.getText()) || mEtNickName.getText().toString().equals(_user.getUserIntroduction())) {
                    finish();
                } else {
                    ApiModifyIntro(mEtNickName.getText().toString().trim());
                }
                break;
            default:
                break;
        }
    }

    private void ApiModifyIntro(final String intro) {
        final Dialog dialog = DialogUtil.getWaitDialog(getContext(), "提交中");
        dialog.show();
        Call<BaseDTO> call = getRequestApi().modifyIntro(intro);
        call.enqueue(new Callback<BaseDTO>() {
            @Override
            public void onResponse(Call<BaseDTO> call, Response<BaseDTO> response) {
                dialog.dismiss();
                if (response.isSuccessful()) {
                    BaseDTO baseDTO = response.body();
                    if (baseDTO != null && response.isSuccessful()) {
                        ToastUtil.showToast("修改成功");
                        _user.setUserIntroduction(intro);
                       /* ChatHelper.sendUpdateUserName(nickName);
                        String clientId = ""+ Global.user.getUserId();
                        ContactHelper.getContact(clientId).setNickName(nickName);*/
                        finish();
                    } else {
                        ToastUtil.showToast(baseDTO.getMsg());
                    }
                } else {
                    ToastUtil.showToast("请求失败");
                }
            }

            @Override
            public void onFailure(Call<BaseDTO> call, Throwable t) {
                dialog.dismiss();
                ToastUtil.showToast("网络故障");
            }
        });
    }
}
